package com.sduept.nwld.dataserver.controller.ahp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.risk.model.ahp.RiskFactor;
import com.sduept.bigdata.risk.model.ahp.RiskLevel;
import com.sduept.bigdata.risk.service.RiskFactorManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.generator.ObjectId;

@Named
@ViewScoped
public class RiskFactorController extends AbstractController {

	private static final long serialVersionUID = -7513110161364638213L;
	@Autowired
	private RiskFactorManager rm;

	private List<RiskFactor> risks = new ArrayList<>();
	private List<RiskLevel> risklevels = new ArrayList<>();
	private RiskLevel riskLevel;

	private RiskFactor riskFactor;
	private String flag;

	@PostConstruct
	public void init() {
		risks = rm.findAllFactor();
		risklevels = rm.findAllRiskLevelOrderById();

	}

	public String generatePName(String pid) {
		String pName = "";
		if (StringUtils.isNotEmpty(pid)) {
			if (risks != null && !risks.isEmpty()) {
				for (RiskFactor risk : risks) {
					if (risk.getId().equals(pid)) {
						pName = risk.getName();
						break;
					}
				}
			}
		}
		return pName;
	}

	public void createNewFactor() {
		flag = "create";
		riskFactor = new RiskFactor();
		riskFactor.setId(ObjectId.get().toString());
	}

	public void deleteFactor() {
		if (riskFactor != null) {
			for (RiskFactor risk : risks) {
				if (risk.getId().equals(riskFactor.getId())) {
//					risks.remove(risk);
					rm.deleteRisk(risk);
					break;
				}
			}
			riskFactor = null;
			addSuccessMessage("删除成功");
		}
		init();
	}

	public void updateNewFactor() {
		if (riskFactor != null) {
			flag = "update";
//			risks.remove(riskFactor);
		}
	}

	public void saveRiskFactor() {
		if ("create".equals(flag)) {
//			risks.add(riskFactor);
			rm.saveRiskFactor(riskFactor);
			addSuccessMessage("新建成功");
		} else if ("update".equals(flag)) {
			if (riskFactor != null) {
//				risks.add(riskFactor);
				rm.saveRiskFactor(riskFactor);
			}
			addSuccessMessage("更新成功");
		}
		riskFactor = null;
		init();
	}

	public void createNewLevel() {
		flag = "create";
		riskLevel = new RiskLevel();
		riskLevel.setId(ObjectId.get().toString());
	}

	public void deleteLevel() {
		if (riskLevel != null) {
			risklevels.remove(riskLevel);
			rm.deleteRiskLevel(riskLevel.getId());
		}
		riskLevel = null;
		addSuccessMessage("删除成功");
	}

	public void updateNewLevel() {
		if (riskLevel != null) {
			flag = "update";
			risklevels.remove(riskLevel);
		}
	}

	public void saveRiskLevel() {
		if ("create".equals(flag)) {
			risklevels.add(riskLevel);
			rm.saveRiskLevel(riskLevel);
			addSuccessMessage("新建成功");
		} else if ("update".equals(flag)) {
			if (riskLevel != null) {
				risklevels.add(riskLevel);
				rm.saveRiskLevel(riskLevel);
			}
			addSuccessMessage("更新成功");
		}
		riskLevel = null;
	}

	public List<RiskFactor> getRisks() {
		return risks;
	}

	public void setRisks(List<RiskFactor> risks) {
		this.risks = risks;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public RiskFactor getRiskFactor() {
		return riskFactor;
	}

	public void setRiskFactor(RiskFactor riskFactor) {
		this.riskFactor = riskFactor;
	}

	public List<RiskLevel> getRisklevels() {
		return risklevels;
	}

	public void setRisklevels(List<RiskLevel> risklevels) {
		this.risklevels = risklevels;
	}

	public RiskLevel getRiskLevel() {
		return riskLevel;
	}

	public void setRiskLevel(RiskLevel riskLevel) {
		this.riskLevel = riskLevel;
	}

}
